package org.brisling.oa;

import static org.junit.Assert.fail;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.PicturesManager;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.hwpf.usermodel.PictureType;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;
import org.w3c.dom.Document;

public class Word2Html {

	@Test
	public void test() {
//		fail("Not yet implemented");
//		try {
//			convert2Html("D:/doc/法人授权委托书.doc", "d:/test/test.html");
//		} catch (TransformerException | IOException | ParserConfigurationException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		
		try {
			docx2Html("D:/doc/湘潭市口腔医院/用户手册.docx", "d:/test/test.html");
		} catch (TransformerException | IOException | ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public void writeFile(String content, String path) {  
        FileOutputStream fos = null;  
        BufferedWriter bw = null;  
        try {  
            File file = new File(path);  
            fos = new FileOutputStream(file);  
            bw = new BufferedWriter(new OutputStreamWriter(fos,"utf-8"));  
            bw.write(content);  
        } catch (FileNotFoundException fnfe) {  
            fnfe.printStackTrace();  
        } catch (IOException ioe) {  
            ioe.printStackTrace();  
        } finally {  
            try {  
                if (bw != null)  
                    bw.close();  
                if (fos != null)  
                    fos.close();  
            } catch (IOException ie) {  
            }  
        }  
    }  
  
    public void convert2Html(String fileName, String outPutFile)  
            throws TransformerException, IOException,  
            ParserConfigurationException {  
        HWPFDocument wordDocument = new HWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));  
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(  
                DocumentBuilderFactory.newInstance().newDocumentBuilder()  
                        .newDocument());  
//         wordToHtmlConverter.setPicturesManager( new PicturesManager()  
//         {  
//             public String savePicture( byte[] content,  
//                     PictureType pictureType, String suggestedName,  
//                     float widthInches, float heightInches )  
//             {  
//                 return "test/"+suggestedName;  
//             }  
//         } );  
        wordToHtmlConverter.processDocument(wordDocument);  
        //save pictures  
//        List<Picture>  pics=wordDocument.getPicturesTable().getAllPictures();  
//        if(pics!=null){  
//            for(int i=0;i<pics.size();i++){  
//                Picture pic = (Picture)pics.get(i);  
//                System.out.println();  
//                try {  
//                    pic.writeImageContent(new FileOutputStream("D:/test/"  
//                            + pic.suggestFullFileName()));  
//                } catch (FileNotFoundException e) {  
//                    e.printStackTrace();  
//                }    
//            }  
//        }  
        Document htmlDocument = wordToHtmlConverter.getDocument();  
        ByteArrayOutputStream out = new ByteArrayOutputStream();  
        DOMSource domSource = new DOMSource(htmlDocument);  
        StreamResult streamResult = new StreamResult(out);  
  
        TransformerFactory tf = TransformerFactory.newInstance();  
        Transformer serializer = tf.newTransformer();  
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");  
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");  
        serializer.setOutputProperty(OutputKeys.METHOD, "html");  
        serializer.transform(domSource, streamResult);  
        out.close();  
        writeFile(new String(out.toByteArray()), outPutFile);  
    }  
    
    private static void docx2Html(String fileName,String targetFileName)  
            throws TransformerException, IOException,  
            ParserConfigurationException { 
    	
    	
        XWPFDocument wordDocument = new XWPFDocument(new FileInputStream(fileName));//WordToHtmlUtils.loadDoc(new FileInputStream(inputFile));  
        XHTMLOptions options = XHTMLOptions.create();
       
        
//        // 存放图片的文件夹
//        options.setExtractor(new FileImageExtractor(new File(imagePathStr)));
//        // html中图片的路径
//        options.URIResolver(new BasicURIResolver("image"));
        
        
        
        OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(targetFileName), "utf-8");
        XHTMLConverter xhtmlConverter = (XHTMLConverter) XHTMLConverter.getInstance();
        xhtmlConverter.convert(wordDocument, out, options);  
         
        out.close();
  
         
          
    } 

}
